package com.wjjun.base.ui.helper

import android.view.View
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent
import androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup
import com.wjjun.base.BR
import com.wjjun.base.ui.listener.IItemViewListener
import com.wjjun.base.ui.viewmodel.*
import me.tatarka.bindingcollectionadapter2.ItemBinding

/**
 * RecycleView 助手类
 * author : wangjunjun
 * date   : 2022/6/8
 * desc   :
 */
open class RecycleViewHelper : IItemViewListener {

    /**
     * 列表的viewModel
     */
    protected var collectionViewModel: CollectionViewModel

    var listItemBinding: ListItemBinding? = null

    var onItemClickListener: IItemViewListener? = null

    //是否可见
    protected var isShow = false

    init {
        collectionViewModel = CollectionViewModel { itemBinding, position, item ->
            onListItemBind(itemBinding, position, item)
        }
        collectionViewModel.setSpanSizeLookup(object : SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                if (position < 0 || position >= collectionViewModel.getData().size) {
                    return 1
                }
                return collectionViewModel.getData()[position]
                    .getItemSpanSize(collectionViewModel.getSpanCount())
            }
        })
    }

    protected open fun onListItemBind(
        itemBinding: ItemBinding<*>,
        position: Int,
        item: BaseItemViewModel<*>
    ) {
        val binding = itemBinding.set(BR.viewModel, item.getItemLayoutId())
        binding.bindExtra(BR.listener, this)
        listItemBinding?.onListItemBind(binding, position, item)
    }

    /**
     * 绑定声明周期
     *
     * @param lifecycleOwner
     */
    fun setLifecycleOwner(lifecycleOwner: LifecycleOwner) {
        lifecycleOwner.lifecycle.addObserver(object : LifecycleObserver {

            @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
            fun onResume() {
                onShow()
            }

            @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
            fun onPause() {
                onHide()
            }

            @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
            fun onDestroy() {
                onLifeDestroy()
            }
        })
    }

    open fun onShow() {
        isShow = true
    }

    open fun onHide() {
        isShow = false
    }

    open fun onLifeDestroy() {
        onItemClickListener = null
        listItemBinding = null
        collectionViewModel.clear()
    }

    override fun onItemClick(view: View, itemViewModel: BaseViewModel) {
        onItemClickListener?.onItemClick(view, itemViewModel)
    }

    /**
     * 设置数据
     */
    open fun setData(itemViewModels: Collection<BaseItemViewModel<*>>?) {
        collectionViewModel.clear()
        if (itemViewModels != null) {
            collectionViewModel.addAll(itemViewModels)
        }
    }

    fun addData(itemViewModels: List<BaseItemViewModel<*>>) {
        collectionViewModel.addAll(itemViewModels)
    }

    fun addItem(index: Int, itemViewModel: BaseItemViewModel<*>) {
        collectionViewModel.addItem(index, itemViewModel)
    }

    fun addItem(itemViewModel: BaseItemViewModel<*>) {
        collectionViewModel.addItem(itemViewModel)
    }

    fun removeAt(index: Int) {
        collectionViewModel.removeAt(index)
    }

    fun removeItem(itemViewModel: BaseItemViewModel<*>) {
        collectionViewModel.remove(itemViewModel)
    }

    fun removeList(list: Collection<BaseItemViewModel<*>>) {
        collectionViewModel.removeAll(list)
    }

    fun clearData() {
        collectionViewModel.clear()
    }

    fun getAllData(): List<BaseItemViewModel<*>> {
        return collectionViewModel.getData()
    }

    open fun isEmpty(): Boolean {
        return collectionViewModel.isEmpty()
    }

    /**
     * 设置缺省viewModel
     */
    fun setEmptyViewModel(viewModel: EmptyItemViewModel) {
        collectionViewModel.emptyListViewModel = viewModel
    }

    /**
     *  设置空界面的图标和文本
     */
    fun setEmpty(icon: Int, msg: String) {
        collectionViewModel.setEmpty(icon, msg)
    }

    /**
     * 获取ViewModel
     */
    fun buildViewModel(): CollectionViewModel {
        return collectionViewModel
    }

    /**
     * 设置recycleView方向
     */
    fun setOrientation(orientation: Int) {
        collectionViewModel.state.orientation.set(orientation)
    }

    /**
     * 设置Grid布局时的spanCount
     */
    fun setSpanCount(spanCount: Int) {
        collectionViewModel.state.spanCount.set(spanCount)
    }

    /**
     * 创建嵌套使用的ViewModel
     */
    fun buildNestViewModel(layoutId: Int): NestCollectionViewModel {
        return NestCollectionViewModel(layoutId).apply {
            setData(collectionViewModel)
        }
    }

    interface ListItemBinding {
        /**
         * 绑定item，已默认绑定viewModel和listener
         * 可用于设置额外的变量
         */
        fun onListItemBind(
            itemBinding: ItemBinding<*>,
            position: Int,
            itemViewModel: BaseItemViewModel<*>
        )

    }


}
